import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 * author:wzq
 * project:01_hello
 * date:16-5-11
 */
public class _04_TaskServiceTest_02 {

    private static ProcessEngine processEngine;
    private static TaskService taskService;
    private static String userA = "aUserNotExist";

    @BeforeClass
    public static void init() {
        processEngine = ProcessEngineConfiguration
                .createStandaloneInMemProcessEngineConfiguration()
                .buildProcessEngine();

        taskService = processEngine.getTaskService();
    }

    @Test
    public void queryTask() {

        // create task
        Task task = taskService.newTask();
        task.setName("taskABC");
        task.setPriority(100);
        taskService.saveTask(task);
        assertNull(task.getAssignee());

        // add candidate to task, NOTE: userA not create
        taskService.addCandidateUser(task.getId(), userA);
        task = taskService.createTaskQuery().taskCandidateUser(userA).singleResult();
        assertNotNull(task);
        assertNull(task.getAssignee());

        // candicate claim task
        taskService.claim(task.getId(), userA);
        task = taskService.createTaskQuery().taskAssignee(userA).singleResult();
        assertEquals(userA, task.getAssignee());
        taskService.complete(task.getId());
        task = taskService.createTaskQuery().taskAssignee(userA).singleResult();
        assertNull(task);
    }
}
